#include "app_buffer.h"
#include "log/log.h"

Buffer *app_buffer_new(int size)
{
    Buffer *buf = malloc(sizeof(Buffer));
    if(buf==NULL){
        log_fatal("buffer malloc failed");
        return NULL;
    }
    buf->total_size = size;
    buf->len=0;
    buf->offset=0;
    buf->ptr = malloc(size);
    if(buf->ptr==NULL){
        log_fatal("ptr malloc failed");
        free(buf);
    }
    log_debug("buffer init success");
    return buf;
}

void app_buffer_free(Buffer *buf)
{
    if(buf==NULL){
        log_warn("buffer is null");
        return;
    }
    if(buf->ptr){
        free(buf->ptr);
    }
    free(buf);
    log_debug("buffer free success");
}

int app_buffer_write(Buffer *buf, char *data, int len)
{
    if(buf==NULL||data==NULL||len<=0){
        log_warn("parameter error");
        return -1;
    }
    if(buf->len+len>buf->total_size){
        log_warn("buffer overflow");
        return -1;
    }
    int write_start = (buf->offset+buf->len)%buf->total_size;
    


}

int app_buffer_read(Buffer *buf, char *data, int len)
{
    return 0;
}
